#include <cstdlib>
#include <cstdio>
#include <pthread.h>
#include "pulseaudioutil.hpp"

static void* start_pa(void* x){
    painitialize((coreinfo*)x);
    return nullptr;

}

int s;
Receiver *receiver;
int main(){
    s = socket(AF_INET, SOCK_DGRAM, 0);
    receiver = new Receiver(s);
    Mixer<Trackreceiver> *trackMixer = new Mixer<Trackreceiver>;
    Trackreceiver *musicTrack = new Trackreceiver();
    Trackreceiver *vocalTrack = new Trackreceiver();
    trackMixer->addSource(musicTrack);
    trackMixer->addSource(vocalTrack);
    // receiveInfo *rinfo = new receiveInfo();
    Channel *music_channel = new Channel(music, channel_unreliable, 1, s, [=](Packet *p){
            musicTrack->receiveSound(p);
            return true; });
    Channel *vocal_channel = new Channel(vocal, channel_unreliable, 1, s, [=](Packet *p){
            vocalTrack->receiveSound(p);
            return true; });
    receiver->addChannel(music_channel);
    receiver->addChannel(vocal_channel);

    // receiver->receive_forever();

    pthread_t pa_thread;
    coreinfo *userdata = new coreinfo;
    userdata->trackMixer = trackMixer;
    userdata->musicTrack = musicTrack;
    userdata->vocalTrack = vocalTrack;
    userdata->musicChannel = music_channel;
    userdata->vocalChannel = vocal_channel;
    userdata->receiver = receiver;
    pthread_create(&pa_thread,NULL,start_pa,userdata);
    // painitialize(userdata);
    receiver->receive_blocking();
}